
module Cms
  class WebPageButton < ToolbarButton

    def action
      fragment = WebPageEditorWindow.new
      rendered = fragment.render
      #scripts = get_scripts fragment
      #p scripts[0].methods

      #id = generate_id
      respond "
        dcat_instance.oninit(function(Y) {
          var parent, html = '#{rendered.join().split("\n").join().split("'").join("\\'")}';
          if (Cat.Page.focused_page) {
            Cat.Page.focused_page.node();
            parent = Cat.Page.focused_page.container;
            console.log('page container is', parent);
            if (parent) {
              parent.appendChild(Y.Node.create(html));
            }
          }
          if (parent == null) {
            parent = '.Desktop > .Container';
            jQuery(parent).append(html);
          }
        });
      "
      #          #{scripts.collect { |s| s.innerHTML }.join }

    end
    
  end
end